package com.sihan.framework.etm.controller.baseData;

import javax.validation.Valid;

import com.framework.annotation.ParamParser;
import com.sihan.framework.etm.common.PageRequest;
import com.sihan.framework.etm.dto.baseData.RecordDTO;
import com.sihan.framework.etm.dto.baseData.RecordListIntervalDTO;
import com.sihan.framework.etm.entity.baseData.DeviceModelDTO;
import com.sihan.framework.etm.entity.business.ManufactorModel;
import com.sihan.framework.etm.mapper.UserMapper;
import com.sihan.framework.etm.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.framework.annotation.BusinessLog;
import com.framework.model.ApiResult;
import com.framework.model.PageResult;
import com.sihan.framework.etm.dto.baseData.DeviceBindDTO;
import com.sihan.framework.etm.dto.baseData.DeviceDTO;
import com.sihan.framework.etm.service.baseData.DeviceService;
import com.sihan.framework.etm.vo.baseData.DeviceVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;


@Api(tags = "设备管理")
@RestController
@RequestMapping("/system/v0/device")
public class DeviceController {

    @Autowired
    private DeviceService deviceService;
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserMapper userMapper;

    @ApiOperation(value = "设备列表", notes = "设备列表", response = DeviceVO.class)
    @GetMapping("/list")
    @BusinessLog(value = "设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "orderBy", value = "排序字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "设备编号", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "设备名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryStartTime", value = "查询开始检", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "queryEndTime", value = "查询结束时间", dataType = "string", paramType = "query")
    })
    //@RequiresPermissions("bs:manager:tool:menu")
    public ApiResult<?> listDevice(DeviceDTO dto) {
        PageResult<DeviceVO> result = deviceService.selectPage(dto);
        return ApiResult.success(result);
    }


    @ApiOperation("保存设备")
    @PostMapping("/save")
    @BusinessLog(value = "保存设备")
    public ApiResult<?> saveDevice(@Valid @ApiParam(name = "deviceDTO", value = "设备数据", required = true)
                                   @RequestBody DeviceDTO dto) {
        deviceService.saveOrUpdateDevice(dto);
        return ApiResult.success();
    }

    @ApiOperation("修改设备")
    @PostMapping("/update")
    @BusinessLog(value = "修改设备")
    public ApiResult<?> editDevice(@Valid @ApiParam(name = "deviceDTO", value = "设备数据", required = true)
                                   @RequestBody DeviceDTO dto) {
        deviceService.saveOrUpdateDevice(dto);
        return ApiResult.success();
    }

    @ApiOperation("设备详情")
    @GetMapping("/detail")
    @BusinessLog(value = "设备详情")
    public ApiResult<?> detail(@ApiParam(name = "id", value = "设备id", required = true) @RequestParam String id) {
        return ApiResult.success(deviceService.selectDeviceById(id));
    }

    @ApiOperation("绑定与解绑")
    @PostMapping("/bind_unbind_device")
    @BusinessLog(value = "绑定与解绑")
    public ApiResult<?> bingOrUnBindDevice(@RequestBody DeviceBindDTO param) {
        deviceService.bindOrUnbindDevice(param.getId(), param.getType());
        return ApiResult.success();
    }

//	@ApiOperation("新增柜子工器具配置")
//	@PostMapping("/tool_config")
//	@BusinessLog(value = "新增柜子工器具配置")
//	public ApiResult<?> addToolConfig(@Valid @ApiParam(name = "DeviceToolConfigDTO", value = "设备数据", required = true)
//	@RequestBody DeviceToolConfigDTO dto) {
//		return deviceService.saveOrUpdateDeviceToolConfig(dto);
//	}
//
//	@ApiOperation("柜子工器具配置详情")
//	@GetMapping("/tool_config_detail")
//	@BusinessLog(value = "柜子工器具配置详情")
//	public ApiResult<?> toolConfigDetail(@ApiParam(name = "id", value ="柜子id", required = true) @RequestParam String id) {
//		return deviceService.selectDeviceToolConfigById(id);
//	}
//
//	@ApiOperation("新增柜子人员配置")
//	@PostMapping("/user_config")
//	@BusinessLog(value = "新增柜子人员配置")
//	public ApiResult<?> addUserConfig(@Valid @ApiParam(name = "DeviceUserConfigDTO", value = "设备数据", required = true)
//	@RequestBody DeviceUserConfigDTO dto) {
//		return deviceService.saveOrUpdateDeviceUserConfig(dto);
//	}
//
//	@ApiOperation("柜子人员配置详情")
//	@GetMapping("/user_config_detail")
//	@BusinessLog(value = "柜子工器具配置详情")
//	public ApiResult<?> userConfigDetail(@ApiParam(name = "id", value ="柜子id", required = true) @RequestParam String id) {
//		return deviceService.selectDeviceUserConfigById(id);
//	}

    @ApiOperation("删除设备")
    @PostMapping("/delete")
    @BusinessLog(value = "删除设备")
    public ApiResult<?> deleteDevice(@ApiParam(name = "id", value = "设备id", required = true) @RequestParam String id) {
        return ApiResult.success(deviceService.deleteDevice(id));
    }

    @ApiOperation("设备记录列表")
    @GetMapping("/recordList")
    @BusinessLog(value = "设备记录列表")
    public ApiResult<?> recordList(RecordDTO dto) {
        return ApiResult.success(deviceService.recordList(dto));
    }

    @ApiOperation("设备记录列表")
    @PostMapping("/recordListInterval")
    @BusinessLog(value = "设备记录列表")
    public ApiResult<?> recordListInterval(@ParamParser @RequestBody RecordListIntervalDTO dto) {
        return ApiResult.success(deviceService.recordListInterval(dto));
    }


    @ApiOperation("分页获取设备列表")
    @GetMapping("/getListDeviceModelDTO")
    @BusinessLog(value = "分页获取设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "设备名称", dataType = "string", required = false),
            @ApiImplicitParam(name = "code", value = "设备编号", dataType = "string", required = false),
            @ApiImplicitParam(name = "toolType", value = "设备类型编号", dataType = "int", required = false)
    })
    public ApiResult<?> getListDeviceModelDTO(PageRequest pageRequest,
                                              String name,
                                              String code,
                                              Integer toolType) {
        return ApiResult.success(deviceService.getListDeviceModelDTO(pageRequest, name, code, toolType));
    }
    @ApiOperation("微信分页获取设备列表")
    @GetMapping("/wx/getListDeviceModelDTO")
    @BusinessLog(value = "微信分页获取设备列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "查询/name/code", dataType = "string", required = false)
    })
    public ApiResult<?> wxGetListDeviceModelDTO(PageRequest pageRequest,
                                              String code) {
        return ApiResult.success(deviceService.wxGetListDeviceModelDTO(pageRequest, code));
    }

    @ApiOperation("获取单个设备信息")
    @GetMapping("/getOneDeviceModelDTO")
    @BusinessLog(value = "获取单个设备信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "deviceId", value = "设备id", dataType = "string", required = true),
    })
    public ApiResult<?> getOneDeviceModelDTO(String deviceId) {
        DeviceModelDTO dto = deviceService.getOneDeviceModelDTO(deviceId);
        if (null == dto){
            DeviceModelDTO deviceModelDTO = new DeviceModelDTO();
            return ApiResult.success(deviceModelDTO);
        }
        return ApiResult.success(dto);
    }


    @ApiOperation("添加厂商")
    @PostMapping("/addManufator")
    @BusinessLog(value = "添加厂商")
    public ApiResult<?> addManufator(@ParamParser @RequestBody ManufactorModel manufactorModel) {
        if (deviceService.addManufator(manufactorModel)){
            return ApiResult.success();
        }
        return ApiResult.fail();
    }

}
